package com.macro.mall.common.api;

import cn.hutool.json.JSONUtil;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * @author super
 * @title: SMSSend
 * @description: TODO
 * @date 2020/8/14 12:05
 */
@Slf4j
@Configuration
public class SMSSend {


    @Value("${sms.url}")
    private static String url;

    public static void main(String[] args) throws Exception {

        String masage = SMSSendCode.appointment.getMasage();
        SMSSend.sendSMS("13121374111",
                masage.replaceAll("xcode","到店").replaceAll("xname","W-到店服务"),
                SMSSendCode.appointment.getCode());

    }


    public static void sendSMS(String phoneNumbers, String code, String templateCode) throws Exception {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI4G9zqaipdMhUToftrCU1", "DGcehb6mMNr08y2qv68uGehnTLRtNp");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");//DateUtil.format(new Date(), "yyyy-MM-dd"));
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", "正德双创");
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", code);
        try {
            log.info(JSONUtil.toJsonStr(request));
            CommonResponse response = client.getCommonResponse(request);

            log.info(response.getData());
        } catch (ServerException e) {
            log.error("", e);
        } catch (ClientException e) {
            log.error("", e);
        }
    }


}
